﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity.Core.EntityClient;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.Configurations
{
    public class Configs
    {
        public static readonly string DbPassport;
        public static readonly string BookStoreEntities;
        public static string ConnectionString
        {
            get
            {
                string _ConnectionString = "Server={0};\r\nDatabase=BookStore;\r\nUser id={1};";
                _ConnectionString = String.Format(_ConnectionString, Server, ConfigurationManager.ConnectionStrings["UserID"].ConnectionString);
                return _ConnectionString;
            }
        }
        public static EntityConnection ConnectionEntity
        {
            get
            {
                string providerName = "System.Data.SqlClient";
                string serverName = Server;
                string databaseName = "BookStore";

                // Initialize the connection string builder for the
                // underlying provider.
                SqlConnectionStringBuilder sqlBuilder =
                    new SqlConnectionStringBuilder();

                // Set the properties for the data source.
                sqlBuilder.DataSource = serverName;
                sqlBuilder.InitialCatalog = databaseName;
                sqlBuilder.UserID = ConfigurationManager.ConnectionStrings["UserID"].ConnectionString;
                sqlBuilder.Password = ConfigurationManager.ConnectionStrings["Password"].ConnectionString;
                // Build the SqlConnection connection string.
                string providerString = sqlBuilder.ToString();

                // Initialize the EntityConnectionStringBuilder.
                EntityConnectionStringBuilder entityBuilder =
                    new EntityConnectionStringBuilder();

                //Set the provider name.
                entityBuilder.Provider = providerName;

                // Set the provider-specific connection string.
                entityBuilder.ProviderConnectionString = providerString;

                // Set the Metadata location.
                entityBuilder.Metadata = @"res://*/BookStore.csdl|res://*/BookStore.ssdl|res://*/BookStore.msl";
                return new EntityConnection(entityBuilder.ToString());
            }
        }
        public static readonly string Database;
        public static readonly string Server;
        public static readonly string FirstLaunch;
        public static readonly string ImageFolderPath;
        public static string UserName { get { return ConfigurationManager.AppSettings["UserName"]; } }
        
        static Configs()
        {
            DbPassport = ConfigurationManager.ConnectionStrings["DbPassport"].ConnectionString;
            BookStoreEntities = ConfigurationManager.ConnectionStrings["BookStoreEntities"].ConnectionString;
            Server = ConfigurationManager.ConnectionStrings["Server"].ConnectionString;
            Database = ConfigurationManager.AppSettings["Database"];
            ImageFolderPath = ConfigurationManager.AppSettings["ImageFolderPath"];
            FirstLaunch = ConfigurationManager.AppSettings["FirstLaunch"];
        }

        public static void EditAppSetting(string key, string value)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[key].Value = value;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        //providerName
        public static void EditConnectionString(string key, string value)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            connectionStringsSection.ConnectionStrings[key].ConnectionString = value;
            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
        }
        public static void EditConnectionString(string key, string value, string providerName)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            connectionStringsSection.ConnectionStrings[key].ConnectionString = value;
            connectionStringsSection.ConnectionStrings[key].ProviderName = providerName;
            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
        }
    }
}
